'use strict'

let sceneMgr = require('../core/sceneMgr')
let playerDao = require('../dao').PlayerDao
let tradeDao = require('../dao').TradeDao
let templateConfig = require('../../config/templateConfig')
let ItemComponent = require('../component/itemComponent')
let attackHandle = require('../core/attackHandle')
let adventure = require('../core/adventure')
let fieldBossComponent = require('../component/fieldBossComponent')
let arenaHandle = require('../component/arenaComponent')
let route = require('./route')
let pushMsg = require('./pushMsg')
let playerSessionMgr = require('../core/playerSessionMgr')
let mailDao = require('../dao').MailDao
let PlayerModel = require('../dao/model').Player
let trialHandle = require('../component/trialComponent')
let guardComponent = require('../component/guardComponent')
let utils = require('../utils/utils')
let gateHandle = require('./gateHandle')
let createMonsterHandle = require('../core/createMonsterHandle')

//开始试炼
route.set('test_trialBattle', function(player, msg, next) {
  trialHandle.trialBattle()
  next({
    error: 0
  })
})

//获取守护之地boss信息
route.set('test_getFieldBossInfo', function(player, msg, next) {
  let guardSceneId = msg.guardSceneId
  let fieldBoss = guardComponent.getFieldBossInfo(guardSceneId)
  next({
    error: 0,
    fieldBoss: fieldBoss
  })
})

//进入守护之地
route.set('test_enterGuard', function(player, msg, next) {
  let guardSceneId = msg.guardSceneId
  sceneMgr.getSceneSerById(guardSceneId).addClient(player)
  next({
    error: 0
  })
})

//攻击守护之地boss
route.set('test_attackFieldBoss', function(player, msg, next) {
  let guardSceneId = msg.guardSceneId
  let fightMsg = guardComponent.attackFieldBoss(player, guardSceneId)
  next({
    error: 0,
    player: player,
    fightMsg: fightMsg
  })
})

//获取装备信息
route.set('test_getEquipmentInfo', function(player, msg, next) {
  next({
    error: 0,
    equipments: player.equipments
  })
})

//设置lattice
route.set('test_setLattice', function(player, msg, next) {
  let lattice = msg.latticeId
  let re = /^[0-9]*]*$/
  if (!re.test(lattice)) {
    next(1)
    return
  }
  lattice = parseInt(msg.latticeId)
  if (lattice > 25) {
    next(2)
    return
  }
  player.basicInfo.lattice = lattice
  next({
    error: 0,
  })
})

//测试_攻击怪物
route.set('test_attackMonster', function(player, msg, next) {
  let fightMsg = attackHandle.attackMonster(player)
  Log(fightMsg)
  next({
    error: 0
  })
})

//测试_攻击副本怪物
route.set('test_attackCaveMonster', function(player, msg, next) {
  let monsterId = msg.monsterId
  let fightMsg = attackHandle.attackCaveMonster(player, monsterId)
    // Log(fightMsg)
  next({
    error: 0
  })
})

//添加物品到背包
route.set('addItem', function(player, msg, next) {
  let itemId = msg.itemId
  let num = parseInt(msg.num)
  if (!num) {
    num = 1
  }
  if (!templateConfig.prop[itemId]) {
    next(1)
    return
  }
  let itemComponent = templateConfig.total[itemId].getItemComponent(num)
  let ret = player.bag.addItem(itemComponent)
  next(0)
  return
})

//添加金钱
route.set('addMoney', function(player, msg, next) {
  let num = msg.num
  player.basicInfo.money += parseInt(num)
  next(0)
  return
})

//添加金币
route.set('test_addGold', function(player, msg, next) {
  let num = msg.num
  player.basicInfo.gold += parseInt(num)
  next(0)
  return
})

//获取所有的配置
route.setNotLogin('test_getTemplateConfig', function(msg, next) {
  next({
    error: 0,
    templateConfig: templateConfig
  })
})

//获取所有的配置
route.set('test_getTemplateConfig', function(player, msg, next) {
  next({
    error: 0,
    templateConfig: templateConfig
  })
})

//获得装备
route.set('test_getEquipment', function(player, msg, next) {
  let type = parseInt(msg.type)
  let lvl = parseInt(msg.lvl)
  let flag = parseInt(msg.flag)
  let secretMapId = msg.secretMapId
  let lucky = msg.lucky
  if (!(type === 51 || type === 52 || type === 53 || type === 54 || type === 55 || type === 60 || type === 61 || type === 62 || type === 63 || type === 64 || type === 65)) {
    next(1)
    return
  }
  if (!lvl || lvl <= 0 || lvl > 15) {
    next(2)
    return
  }

  let equipment = createMonsterHandle.test_createEquipment(type, lvl, flag, secretMapId, lucky)
  player.bag.addItem(equipment)
    // Log(fightMsg)
  next({
    error: 0,
    equipment: equipment
  })
})

//获得冒险背包信息
route.set('test_getAdventureBag', function(player, msg, next) {
  next({
    error: 0,
    adventureBag: player.adventureBag
  })
})

//添加物品到冒险背包
route.set('test_addItemABag', function(session, msg, next) {
  let itemId = msg.itemId
  if (!templateConfig.prop[itemId]) {
    next(1)
    return
  }
  let itemComponent = templateConfig.total[itemId].getItemComponent()
  player.adventureBag.push(itemComponent)
  next(0)
  return
})

//测试_提升等级
route.set('test_lvlup', function(player, msg, next) {
  player.basicInfo.lvl += 1
  let characterConfig = templateConfig.character[player.templateId]
  player.basicInfo.attriPoint += characterConfig.lvlUpAttri.attriPoint
  player.calcAttri()
    // Log(fightMsg)
  next({
    error: 0,
    player: player
  })
})

//测试_降低等级
route.set('test_lvldown', function(player, msg, next) {
  if (player.basicInfo.lvl === 1) {
    next(1)
    return
  }
  player.basicInfo.lvl -= 1
  player.calcAttri()
    // Log(fightMsg)
  next({
    error: 0,
    player: player
  })
})

//测试_删除所有用户
route.setNotLogin('test_deleteUser', function(player, msg, next) {
  let name = msg.name
    //判断名字是否已经存在
  playerDao.removePlayerByQuery('', function(err, res) {
    next(0)
    return
  })
})

//测试_格子数据
route.setNotLogin('test_latticeData', function(player, msg, next) {
  let secretMapId = msg.secretMapId
  let secretMapConfig = templateConfig.secretMap[secretMapId]
  if (!secretMapConfig) {
    next(1)
    return
  }
  let latticeMap = secretMapConfig.latticeMap
  let mapArrays = []
  for (let i in latticeMap) {
    let maps = latticeMap[i]
    for (let j in maps) {
      let lattices = maps[j]
      let array = ''
      for (let k in lattices) {
        let latticeId = lattices[k]
        if (templateConfig.lattice[latticeId]) {
          let latticeName = templateConfig.lattice[latticeId].name
          array += latticeName + ' '
        } else {
          array += latticeId + ' '
        }
      }
      mapArrays.push(array)
    }
    mapArrays.push('----------------')
  }
  next({
    error: 0,
    mapArrays: mapArrays
  })
})

//发送邮件
route.set('test_sendMail', function(player, msg, next) {
  let item = templateConfig.prop[msg.itemId]
  if (!item) {
    next({
      error: 1
    })
    return
  }
  let re = /^[0-9]*]*$/
  if (!re.test(msg.menoy) || !re.test(msg.gold)) {
    next({
      error: 2
    })
    return
  }
  mailDao.newAndSave(msg.playerId, msg.title, msg.content, msg.itemId, parseInt(msg.menoy), parseInt(msg.gold), function(err) {
    if (!err) {
      next({
        error: 0
      })
      pushMsg.newMail(msg.playerId)
    } else {
      next({
        error: 1
      })
    }

  })
})

//保存角色
route.set('test_savePlayer', function(player, msg, next) {
  playerDao.savePlayerById(player._id, player, function(err, ret) {
    let error = err ? 1 : 0
    next({
      error: error,
      ret: ret
    })
  })
})

//保存角色
route.set('test_arenaReward', function(player, msg, next) {
  arenaHandle.reward()
  next(0)
})
